package com.scm.controller;

import com.scm.service.FactoryService;
import com.scm.pojo.ScmUser;
import com.scm.pojo.UserModel;
import com.scm.pojo.UtilUser;
import com.scm.service.ScmService;
import com.scm.service.UserModelService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName UserController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/6 0:41
 * @Version 1.0
 **/
@RestController
public class UserController {
    //@ResponseBody 把 字符串转换成json串发送
//    @RequestBody 方法里 把传过来的json串封装成对象

//    @ResponseBody+   @Controller  @RestController

    @Autowired
    private UtilUser utilUser;

    @Autowired
    private ScmService scmService;

    @Autowired
  private UserModelService userModelService;

    @Autowired
    private FactoryService factoryService;
    @RequestMapping("users")
    public QueryResult list(Integer currentPage, Integer pageSize){
        Page page = new Page(currentPage,pageSize);
        int total= scmService.countAll();
        Map<String ,Object> params = new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<ScmUser> list = scmService.findAll(params);
        utilUser.setModel(list);

        return QueryResult.ok(total,list);

    }

    @RequestMapping("addUser")
    public Result addUser(ScmUser scmUser, String powers){
        factoryService.addUser(scmUser,powers);
        return Result.ok(null);
    }
    @RequestMapping("deleteUser")
    public Result deleteUser(String account){
        String userName = scmService.getUserName(account);
        ScmUser user = scmService.findUser(userName);
        if (user.getStatus().equals("2")){
            return Result.error("已锁定无法删除");
        }else {
            factoryService.deleteUser(account);
            return Result.ok(null);
        }

    }

    @RequestMapping("/updateUser")
    public Result updateUser(String name){
        ScmUser scmUser = scmService.findUser(name);
        List<UserModel> list = userModelService.getUserModel(scmUser.getAccount());
        scmUser.setPower(list);
        System.out.println(scmUser);
        return Result.ok(scmUser);
    }

    @RequestMapping("/updateUser2")
    public Result updateUser2(ScmUser user,String powers){
       factoryService.updateUser(user,powers);
//        //删除对应权限
//        String account = scmService.findAccount(user.getAccount2());
//        userModelService.deleteModel(account);
//        //修改用户
//        scmService.updateUser(user);
//        //重新添加权限
//        String[] split = powers.split(",");
//        for (String s :split){
//            userModelService.addModel(new UserModel(user.getAccount(),s));
//        }
        return Result.ok(null);
    }
}